package com.shanling.shanlingcontroller.ui.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.BluetoothState;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.MybluetoothDevice;
import com.shanling.shanlingcontroller.services.csr.MainGaiaManager;
import com.shanling.shanlingcontroller.ui.fragment.BleStatusFragment;
import com.shanling.shanlingcontroller.ui.fragment.DeviceFragment;
import com.shanling.shanlingcontroller.ui.fragment.EQFragment;
import com.shanling.shanlingcontroller.ui.fragment.FilterFragment;
import com.shanling.shanlingcontroller.ui.fragment.InstructionsFragment;
import com.shanling.shanlingcontroller.ui.fragment.MusicFragment;
import com.shanling.shanlingcontroller.utils.DpUtils;
import com.shanling.shanlingcontroller.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BleDevicecontrolActivity extends BaseAppCompatActivity implements RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.switch_eq)
    Switch eqSwitch;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.fl)
    FrameLayout fl;
    @BindView(R.id.rb_status)
    RadioButton rbStatus;
    @BindView(R.id.rb_eq)
    RadioButton rbEq;
    @BindView(R.id.rb_filter)
    RadioButton rbFilter;
    @BindView(R.id.rb_directions)
    RadioButton rbDirections;
    @BindView(R.id.rg)
    RadioGroup rg;
    private BleStatusFragment bleStatusFragment;
    private EQFragment eqFragment;
    private FilterFragment filterFragment;
    private InstructionsFragment instructionsFragment;
    private FragmentTransaction transaction;
    private FragmentManager fManager;//管理Fragment界面
    @Override
    protected void getBundleExtras(Bundle extras) {

    }
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_ble_devicecontrol;
    }
    @Override
    protected void initViewsAndEvents() {
        fManager = getSupportFragmentManager();
        Drawable drawable_status = getResources().getDrawable(R.drawable.tab_icon_status_selector);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_status.setBounds(0, 0, DpUtils.dpToPx(this,36),  DpUtils.dpToPx(this,36));
        //设置图片在文字的哪个方向
        rbStatus.setCompoundDrawables(null, drawable_status, null, null);
        Drawable drawable_eq = getResources().getDrawable(R.drawable.tab_icon_eq_selector);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_eq.setBounds(0, 0, DpUtils.dpToPx(this,36),  DpUtils.dpToPx(this,36));
        //设置图片在文字的哪个方向
        rbEq.setCompoundDrawables(null, drawable_eq, null, null);
        Drawable drawable_filter = getResources().getDrawable(R.drawable.tab_icon_filter_selector);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_filter.setBounds(0, 0, DpUtils.dpToPx(this,36),  DpUtils.dpToPx(this,36));
        //设置图片在文字的哪个方向
        rbFilter.setCompoundDrawables(null, drawable_filter, null, null);
        Drawable drawable_directions = getResources().getDrawable(R.drawable.tab_icon_directions_selector);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_directions.setBounds(0, 0, DpUtils.dpToPx(this,36),  DpUtils.dpToPx(this,36));
        //设置图片在文字的哪个方向
        rbDirections.setCompoundDrawables(null, drawable_directions, null, null);
        setChoiceItem(0);
        rg.setOnCheckedChangeListener(this);
        eqSwitch.setOnCheckedChangeListener(this);

    }
    private void setChoiceItem(int index) {
        transaction = fManager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case 0:
                tvTitle.setText(R.string.status);
                eqSwitch.setVisibility(View.GONE);
                if (bleStatusFragment == null) {
                    bleStatusFragment = new BleStatusFragment();
                    transaction.add(R.id.fl, bleStatusFragment);
                } else {
                    transaction.show(bleStatusFragment);
                }
                break;
            case 1://
                tvTitle.setText(R.string.Equalizer);
                eqSwitch.setVisibility(View.VISIBLE);
                if (eqFragment == null) {
                    eqFragment = new EQFragment();
                    transaction.add(R.id.fl, eqFragment);
                } else {
                    transaction.show(eqFragment);
                }
                break;
            case 2://
                tvTitle.setText(R.string.filter);
                eqSwitch.setVisibility(View.GONE);
                if (filterFragment == null) {
                    filterFragment = new FilterFragment();
                    transaction.add(R.id.fl, filterFragment);
                } else {
                    transaction.show(filterFragment);
                }
                break;
            case 3://
                tvTitle.setText(R.string.Instructions);
                eqSwitch.setVisibility(View.GONE);
                if (instructionsFragment == null) {
                    instructionsFragment = new InstructionsFragment();
                    transaction.add(R.id.fl, instructionsFragment);
                } else {
                    transaction.show(instructionsFragment);
                }
                break;
        }
        transaction.commit();
    }


    /**
     * 隐藏片段
     *
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (bleStatusFragment != null) {
            transaction.hide(bleStatusFragment);
        }
        if (eqFragment != null) {
            transaction.hide(eqFragment);
        }
        if (filterFragment != null) {
            transaction.hide(filterFragment);
        }
        if (instructionsFragment != null) {
            transaction.hide(instructionsFragment);
        }
    }








    @Override
    protected void initData() {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }
    @Override
    protected void onEventComing(EventCenter eventCenter) {
        switch (eventCenter.getEventCode()) {
            case Constants.BLUETOOTH_STATE:
                BluetoothState state = (BluetoothState) eventCenter.getData();
                if (state == null ) {
                    return;
                }
                switch (state.getState()){
                    case Constants.DISCONNECTED:
                        ToastUtils.showLongToast(customApplication, R.string.bluetooth_disconnected);
                        finish();
                        break;
                }
                break;
            case Constants.GET_EQ_SELECTED:
               eqSwitch.setChecked((Boolean) eventCenter.getData());
                break;
        }
    }
    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.rb_status:
                setChoiceItem(0);
                break;
            case R.id.rb_eq:
                setChoiceItem(1);
                break;
            case R.id.rb_filter:
                setChoiceItem(2);
                break;
            case R.id.rb_directions:
                setChoiceItem(3);
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        EventBus.getDefault().postSticky(new EventCenter<>(Constants.SET_EQ_SELECTED,isChecked));
    }
}
